package com.suray.system.run.seer.mysql;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.suray.system.run.seer.type.PointMapType;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class PointMapServiceImpl extends ServiceImpl<PointMapMapper, PointMapDB> implements PointMapService {

    @Resource
    PointMapMapper portMapMapper;

    @Override
    public boolean addPortMap(PointMapDB portMapDB) {
        return this.save(portMapDB);
    }

    @Override
    public boolean deletePortMapById(List<Integer> ids) {
        portMapMapper.deleteBatchIds(ids);
        return true;
    }

    @Override
    public PointMapDB getPortMapByWmsNode(String wmsNode) {
        List<PointMapDB> portMapDBS = this.list();

        for (PointMapDB portMapDB : portMapDBS) {
            if (portMapDB.getWmsPoint().equals(wmsNode)) {
                return portMapDB;
            }
        }

        return null;
    }

    @Override
    public PointMapDB getPortMapByPlcNode(String plcNode) {
        List<PointMapDB> portMapDBS = this.list();

        for (PointMapDB portMapDB : portMapDBS) {
            if (portMapDB.getPlcPoint().equals(plcNode)) {
                return portMapDB;
            }
        }

        return null;
    }

    @Override
    public PointMapDB getPortMapByAgvNode(String agvNode) {
        List<PointMapDB> portMapDBS = this.list();

        for (PointMapDB portMapDB : portMapDBS) {
            if (portMapDB.getAgvPoint().equals(agvNode)) {
                return portMapDB;
            }
        }

        return null;
    }

    /**
     * 根据库区获取对应的portmap
     *
     * @param district：wms下发任务的库区（EJ003, EJ004, EJ005）
     * @return
     */
    @Override
    public List<PointMapDB> getPortMapsByDist(String district) {
        List<PointMapDB> portMapDBS = this.list();
        List<PointMapDB> distPortMaps = new ArrayList<>();

        for (PointMapDB portMapDB : portMapDBS) {
            if (district.contains(portMapDB.getDistrict())) {
                distPortMaps.add(portMapDB);
            }
        }

        return distPortMaps;
    }

    @Override
    public PointMapDB getPortMapByMapNode(String mapNode) {
        QueryWrapper<PointMapDB> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(PointMapDB::getMapNode, mapNode);
        List<PointMapDB> resultList = baseMapper.selectList(queryWrapper);

        return resultList.get(0);
    }

    @Override
    public IPage<PointMapDB> getPortMaPDBByPage(PointMapDB portMapDB) {
        Page page = new Page(portMapDB.getCurPage(), portMapDB.getPageSize());
        QueryWrapper<PointMapDB> queryWrapper = new QueryWrapper<PointMapDB>();
        if (portMapDB.getDistrict() != null && !portMapDB.getDistrict().equals("")) {
            queryWrapper.lambda().eq(PointMapDB::getDistrict, portMapDB.getDistrict());
        }
        return portMapMapper.selectPage(page, queryWrapper);
    }

    @Override
    public List<PointMapDB> findPointMapDBCloaseRgv() {
        QueryWrapper<PointMapDB> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(PointMapDB::getType, PointMapType.CONNECT_TYPE).or()
                .eq(PointMapDB::getType, PointMapType.LIFT_TYPE);

        return list(queryWrapper);
    }
}
